﻿using Kernelx.Common.Clauses;
using Kernelx.Common.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Kernelx.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class LambdaExpressionExtensions
    {
        #region Clause2Expression

        /// <summary> 
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static LambdaExpression CreateLambdaExpression(this IClauseElement element, Type targetType, ParameterExpression parameter = null)
            => _ServiceContainer.GetClauseConverter().CreateLambdaExpression(element, targetType, parameter);


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="element"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> CreateExpression<T>(this IClauseElement element, ParameterExpression parameter = null)
            => element?.CreateLambdaExpression(typeof(T), parameter) as Expression<Func<T, bool>>;


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="element"></param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> ToExpression<T>(this IClauseElement element) where T : class
            => element?.CreateExpression<T>();


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="elements"></param>
        /// <returns></returns>
        public static IEnumerable<Expression<Func<T, bool>>> ToExpression<T>(this IEnumerable<IClauseElement> elements) where T : class
            => elements?.Select(x => x.ToExpression<T>());

#if NET35
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="elements"></param>
        /// <returns></returns>
        public static IEnumerable<Expression<Func<T, bool>>> ToExpression<T>(this IEnumerable<ClauseElement> elements) where T : class
            => elements?.Select(x => x.ToExpression<T>());
#endif



        #endregion

        #region Expression2Clause

        /// <summary> 
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expr"></param>
        /// <returns></returns>
        public static IClauseElement ToClauseElement<T>(this Expression<Func<T, bool>> expr)
            => _ServiceContainer.GetClauseConverter().CreateClauseElement(expr?.Body);

        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <param name="operator"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IClauseElement CreateClauseElement(this ClauseOperator @operator, string propertyName, object value)
            => new ClauseElement { PropName = propertyName, Operator = @operator, Value = value };

    }
}
